package br.faimg.pomar.visual;

import br.faimg.pomar.controle.BusinessDelegate.ProducaoBusinessDelegate;
import br.faimg.pomar.controle.BusinessDelegate.TipodeErroBusinessDelegate;
import java.awt.event.KeyEvent;

/*
 * CadastroDefeito.java
 *
 * Created on 17 de Agosto de 2008, 14:28
 */
/**
 *
 * @author  diogo
 */
public class CadastroDefeito extends javax.swing.JDialog {

    String codigoDefeito;
    String codigoEstacao;
    private String numerodeSerie;

    /** Creates new form CadastroDefeito */
    public CadastroDefeito(String codigodefeito, String codigoestacao) {
        initComponents();

        TipodeErroBusinessDelegate delegate = new TipodeErroBusinessDelegate();
        descricaoDefeitpojTextArea.insert(delegate.readById(codigodefeito).getDescricao(), 0);

        codigoDefeito = codigodefeito;
        codigoEstacao = codigoestacao;

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        numerodeSeriejTextField = new javax.swing.JTextField();
        cancelarjButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        descricaoDefeitpojTextArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Informe de defeito");
        setAlwaysOnTop(true);
        setMinimumSize(new java.awt.Dimension(400, 200));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setText("DESCRIÇÃO DO DEFEITO");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        jLabel1.setText("NUMERO DE SÉRIE");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 10, -1, -1));

        numerodeSeriejTextField.setFont(new java.awt.Font("UnDotum", 1, 18));
        numerodeSeriejTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                numerodeSeriejTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(numerodeSeriejTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 30, 260, 40));

        cancelarjButton.setText("Cancelar");
        cancelarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(cancelarjButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 100, 260, -1));

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jScrollPane1.setFocusable(false);

        descricaoDefeitpojTextArea.setBackground(new java.awt.Color(255, 0, 29));
        descricaoDefeitpojTextArea.setColumns(20);
        descricaoDefeitpojTextArea.setEditable(false);
        descricaoDefeitpojTextArea.setFont(new java.awt.Font("Arial Black", 2, 18));
        descricaoDefeitpojTextArea.setForeground(new java.awt.Color(254, 254, 254));
        descricaoDefeitpojTextArea.setRows(5);
        descricaoDefeitpojTextArea.setFocusable(false);
        jScrollPane1.setViewportView(descricaoDefeitpojTextArea);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 430, 100));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 740, 140));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void numerodeSeriejTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_numerodeSeriejTextFieldKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {

        //Inserir o codigo de validacao do produto
        ProducaoBusinessDelegate delegate = new ProducaoBusinessDelegate();

        //Leitura de erro em um estacação de teste


        try {
            delegate.createProdutoComErro(numerodeSeriejTextField.getText(), codigoDefeito, codigoEstacao);
            numerodeSerie = numerodeSeriejTextField.getText();
            
            this.setVisible(false);

        } catch (Throwable e) {

            ErrorView view = new ErrorView(e.toString());
            view.setModal(true);
            view.setLocationRelativeTo(null);
            view.setVisible(true);

            numerodeSeriejTextField.setText("");
            numerodeSeriejTextField.requestFocus(true);
        }

    }
}//GEN-LAST:event_numerodeSeriejTextFieldKeyPressed

private void cancelarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarjButtonActionPerformed

    this.setVisible(false);
}//GEN-LAST:event_cancelarjButtonActionPerformed
    /**
     * @param args the command line arguments
     */
//Comentado para testes
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new CadastroDefeito().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelarjButton;
    private javax.swing.JTextArea descricaoDefeitpojTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField numerodeSeriejTextField;
    // End of variables declaration//GEN-END:variables

    public String getNumerodeSerie() {
        return numerodeSerie;
    }
}
